package com.lgh.hotel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lgh.hotel.dao.mapper.TableMapper;
import com.lgh.hotel.dao.pojo.Guest;
import com.lgh.hotel.dao.pojo.RoomType;
import com.lgh.hotel.dao.pojo.SysUser;
import com.lgh.hotel.dao.pojo.Table;
import com.lgh.hotel.service.*;
import com.lgh.hotel.utils.JWTUtils;
import com.lgh.hotel.vo.Result;
import com.lgh.hotel.vo.TableVo;
import com.lgh.hotel.vo.params.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@Transactional
public class TableServerImpl implements TableServer {

    @Autowired
    TableMapper tableMapper;

    @Autowired
    RoomTypeService roomTypeService;

    @Autowired
    GuestService guestService;
    @Autowired
    SysUserService sysUserService;

    @Autowired
    StatisticsService statisticsService;
    @Autowired
    PunchService punchService;

    @Override
    public Result findTableNoState() {
        LambdaQueryWrapper<Table> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Table::getState,0);
        List<Table> tables = tableMapper.selectList(queryWrapper);
        return Result.succ(copyList(tables,true,false,false));
    }

    @Override
    public Result findTableRoomTypeByIdAll(Long id) {
        LambdaQueryWrapper<Table> tableLambdaQueryWrapper = new LambdaQueryWrapper<>();
        tableLambdaQueryWrapper.eq(Table::getState,0).eq(Table::getRoomType,id);
        List<Table> tables = tableMapper.selectList(tableLambdaQueryWrapper);
        return Result.succ(copyList(tables,true,false,false));
    }

    @Override
    public Result findTbalePagesList(PageParams params) {
        //newpage分页参数 第几页 页面大小
        Page<Table> page = new Page<>(params.getPage(), params.getPageSize());
//        LambdaQueryWrapper<Table> queryWrapper = new LambdaQueryWrapper<>();

        //查询分页的数据
        Page<Table> tablePage = tableMapper.selectPage(page, null);
        Page<TableVo> tableVos = toTableVo(tablePage);
        return Result.succ(tableVos);
    }

    private Page<TableVo> toTableVo(Page<Table> tablePage) {
        Page<TableVo> page = new Page<>();

        List<Table> records = tablePage.getRecords();
        List<TableVo> tableVos = new ArrayList<>();
        BeanUtils.copyProperties(tablePage,page);

        for (Table record : records) {
            tableVos.add(copy(record,true,true,false));
        }
        page.setRecords(tableVos);
        return  page;
    }

    @Override
    public Result deteleTable(Long id) {


        Table table = tableMapper.selectById(id);

        if (table.getIdCard() == null || table.getIdCard()== 0){
            return Result.succ(tableMapper.deleteById(id));
        }


        return Result.fail("有住户不能删除");

    }

    @Override
    public Result findTableAll() {

        List<Table> tables = tableMapper.selectList(null);

        List<TableVo> tableVos = copyList(tables,true,true,true);

        return Result.succ(tableVos);
    }

    @Override
    public Result updateTable(TableParamsa tableParams,String token,boolean isTableParams) {

        System.out.println(tableParams);

        Map<String, Object> map = JWTUtils.checkToken(token);
        String userId = (String) map.get("userId");

        Long id = tableParams.getId();
        Table table = tableMapper.selectById(id);
        table.setUpdateThe(userId);
        table.setUpdateTime(System.currentTimeMillis());
        table.setMoney(tableParams.getMoney());
        table.setRoom(tableParams.getRoom());
        table.setRoomType(tableParams.getRoomType());
        table.setDiscount(tableParams.getDiscount());
        table.setContext(tableParams.getContext());
        System.out.println(userId);

        if (isTableParams){
        }

        return Result.succ(tableMapper.updateById(table));
    }

    @Override
    public Result addTable(TableParam tableParam) {

        List<Date> date = tableParam.getDate();
        Date date1 = date.get(0);
        Date date2 = date.get(1);
        long ks = date1.getTime();
        long js = date2.getTime();

        LambdaQueryWrapper<Table> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Table::getRoom,tableParam.getId());
        Table table1 = tableMapper.selectOne(queryWrapper);

        //拿到房间信息
        Table table = tableMapper.selectById(table1.getId());

        //写入入住信息
        //身份证 入住状态
        table.setIdCard(tableParam.getCardId());
        table.setState(1);
        table.setDeposit(tableParam.getDeposit());

        //更新房间信息
        tableMapper.updateById(table);

        //创建客户
        Guest guest = new Guest();
        guest.setName(tableParam.getName());
        guest.setIdCard(tableParam.getCardId());
        guest.setStartingTime(ks);
        guest.setEndTime(js);
        guest.setPhone(tableParam.getPhone());

        //写入用户
        guestService.addGuest(guest);

        statisticsService.add();
        return Result.succ(null);

    }

    @Override
    public Result addTables(TableParamss tableParams, String token) {
        Table table = new Table();
        BeanUtils.copyProperties(tableParams,table);
        SysUser userByToken = sysUserService.findUserByToken(token);
        table.setFounder(userByToken.getFounder());
        table.setCreationTime(System.currentTimeMillis());
        table.setState(0);
        table.setNumber(0);
        return Result.succ(tableMapper.insert(table));

    }

    @Override
    public Result beysucc(TableParam tableParam) {
        List<Date> date = tableParam.getDate();
        Date date1 = date.get(0);
        Date date2 = date.get(1);
        long ks = date1.getTime();
        long js = date2.getTime();

        //拿到房间信息
        Table table = tableMapper.selectById(tableParam.getId());

        //写入入住信息
        //身份证 入住状态
        table.setIdCard(tableParam.getCardId());
        table.setState(1);
        table.setDeposit(tableParam.getDeposit());

        //更新房间信息
        tableMapper.updateById(table);

        //创建客户
        Guest guest = new Guest();
        guest.setName(tableParam.getName());
        guest.setIdCard(tableParam.getCardId());
        guest.setStartingTime(ks);
        guest.setEndTime(js);
        guest.setPhone(tableParam.getPhone());

        //写入用户
        guestService.addGuest(guest);

        return Result.succ(null);

    }

    @Override
    public void findTableByCard(Long idCard) {

        LambdaQueryWrapper<Table> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Table::getIdCard,idCard);

//        Table table = tableMapper.selectOne(queryWrapper);
        List<Table> tables = tableMapper.selectList(queryWrapper);

        System.out.println(tables);
        for (Table table : tables) {

            table.setState(0);
            table.setIdCard(0L);

            tableMapper.updateById(table);
        }

    }

    @Override
    public Result tuifang(Long id) {

        System.out.println(id);
        Table table = tableMapper.selectById(id);
        System.out.println(table);
        Guest guestById = guestService.findGuestByIdCard(table.getIdCard());
        System.out.println(guestById);
        Long startingTime = guestById.getStartingTime();
        Long endTime = guestById.getEndTime();
        long sj = endTime-startingTime;
        System.out.println(sj);
        long a = sj/(57600000L);
        float money = table.getMoney();
        float money2 = (int)a * money;

        table.setState(0);
        table.setIdCard(0L);


        //清理住房表
        tableMapper.updateById(table);
        //清理客户表
        guestService.deleteGuestA(guestById);

        //清理打卡表
        punchService.deletepunch(table.getIdCard());

        //结算时间 a 天数
        System.out.println(a);
        return Result.succ(money2);
    }

    @Override
    public boolean findTableRoomTypeState(Long id) {

        LambdaQueryWrapper<Table> tableLambdaQueryWrapper = new LambdaQueryWrapper<>();

        tableLambdaQueryWrapper.eq(Table::getRoomType,id).eq(Table::getState,1);

        List<Table> tables = tableMapper.selectList(tableLambdaQueryWrapper);

        System.out.println(tables);
        if (tables == null || tables.size() == 0){
            LambdaQueryWrapper<Table> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Table::getRoomType,id);
            List<Table> tables1 = tableMapper.selectList(queryWrapper);
            for (Table table : tables1) {
                tableMapper.deleteById(table.getId());
            }
            return true;
        }

        return false;
    }

    @Override
    public Table findTableByIdCard(Long idCard) {

        LambdaQueryWrapper<Table> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Table::getIdCard,idCard);

        return tableMapper.selectOne(queryWrapper);
    }

    @Override
    public Result yiqingruzhu(TableParam tableParam) {


        //拿到房间信息
        Table table = tableMapper.selectById(tableParam.getId());

        //写入入住信息
        //身份证 入住状态
        table.setIdCard(tableParam.getCardId());
        table.setState(1);
        table.setDeposit(tableParam.getDeposit());

        //更新房间信息
        tableMapper.updateById(table);

        //创建客户
        Guest guest = new Guest();
        guest.setName(tableParam.getName());
        guest.setIdCard(tableParam.getCardId());
        long timeMillis = System.currentTimeMillis();
        long en = timeMillis + (1000*60*24*14);
        guest.setStartingTime(timeMillis);
        guest.setEndTime(en);
        guest.setPhone(tableParam.getPhone());
        //写入用户
        return Result.succ(guestService.addGuest(guest));
    }

    private List<TableVo> copyList(List<Table> tables,boolean isRoomType,boolean isName,boolean isTime) {
        List<TableVo> tableVos = new ArrayList<>();
        for (Table table : tables) {
            tableVos.add(copy(table,isRoomType,isName,isTime));
        }
        return tableVos;
    }

    private TableVo copy(Table table,boolean isRoomType,boolean isName,boolean isTime) {
        TableVo tableVo = new TableVo();
        BeanUtils.copyProperties(table,tableVo);
        if (isRoomType){
            Long roomType = table.getRoomType();
            RoomType roomTypeById = roomTypeService.findRoomTypeById(roomType);
            if (null != roomTypeById) {
                RoomParams roomParams = new RoomParams();
                roomParams.setNumber(roomTypeById.getNumber());
                roomParams.setRemark(roomTypeById.getRemark());
                tableVo.setRoomParams(roomParams);
            }
        }
        if (isName){
            Long idCard = table.getIdCard();

            if (idCard != null && idCard != 0){
                Guest guestById = guestService.findGuestById(idCard);
                System.out.println(table);
                System.out.println(guestById);
                tableVo.setName(guestById.getName());

                Long startingTime = guestById.getStartingTime();
                Long endTime = guestById.getEndTime();
                Date date = new Date(startingTime);
                Date date1 = new Date(endTime);
                SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                String format = sd.format(date);
                String format1 = sd.format(date1);

                tableVo.setStime(format);
                tableVo.setEtime(format1);

            }
//            Guest guestById = guestService.findGuestById(idCard);
//            System.out.println(table);
//            System.out.println(guestById);
////            tableVo.setName(guestById.getName());
        }

        return tableVo;
    }

}
